<?php
namespace backend\logic;

use external\SFTools;

class OauthLogic {
    /**
     * 获取订单列明细
     * @param   string $fccode 订单号
     * @param   string $phone  电话号码
     * @return  array          数组对象数据
     */
    public function getOauthFindBuyFccodeAndPhone($fccode, $phone){
        if (empty($fccode)) {
            return SFTools::err("3903");
        }
        if (!preg_match(PREG_MATCH_PHONE, $phone)) {
            return SFTools::err("3311");
        }
        $cstMst = new \backend\model_erp\CstMst;
        $field = "fCCode,fCNName,fCName,fAddress,fTel";
        $cstMstFind = $cstMst::field($field)->where('fCCode', $fccode)->find();
        if (empty($cstMstFind)) {
            return SFTools::err("3902");
        }
        $fTel = trim($cstMstFind['fTel']);
        if (!preg_match(PREG_MATCH_PHONE, $fTel)) {
            return SFTools::err("3901");
        }
        // 生成 cache_key
        $cache_key = md5("LO{$fccode}{$phone}");
        // 获取缓存对象
        $cache_arr = SFTools::getCache($cache_key);
        $seconds = 60;
        if (!empty($cache_arr)) {
            $seconds = time() - intval($cache_arr['timestamp']);
        }
        if (empty($cache_arr) || $seconds > 60) {
            //$sms_templates = config('sms_templates');
            // 检测经销商是否已经授权过该用户
            $userAuthLogic = new \backend\logic\UserAuthLogic;
            $userAuthFind = $userAuthLogic->getUserAuthFindBuyFccodeAndPhone($fccode, $phone);
            $message = new \external\Message;
            if (empty($userAuthFind) && $phone != $fTel) {
                $pow_number = SFTools::getRandStr(4, 3);
                $pow_phone = substr($fTel, 0, 3) . "*****" . substr($fTel, -3);
                $arr['pow_number'] = $pow_number;
                $arr['pow_phone'] = $pow_phone;
                $info['pow_number'] = $pow_number;
                $info['pow_phone'] = $pow_phone;
                $pow_code = SFTools::getRandStr(6, 1);
                $cache_arr['pow_code'] = $pow_code;
                // 发送授权人短信验证码
                $pow_params['phone'] = $fTel;
                $pow_params['code'] = $pow_code;
                $pow_params['number'] = $pow_number;
                $pow_params['fromPhome'] = $phone;
                $pow_params['template_type'] = 1;
                $message->send($pow_params);
            }
            $cur_number = SFTools::getRandStr(4, 3);
            $cur_phone = substr($phone, 0, 3) . "*****" . substr($phone, -3);
            $arr['cur_number'] = $cur_number;
            $arr['cur_phone'] = $cur_phone;
            $arr['seconds'] = NULL;
            $info['cur_number'] = $cur_number;
            $info['cur_phone'] = $cur_phone;
            $cache_arr['info'] = $info;
            $cur_code = SFTools::getRandStr(5, 1);
            $cache_arr['cur_code'] = $cur_code;
            $cache_arr['timestamp'] = time();
            // 发送登入手机号码短信验证码
            $cur_params['phone'] = $phone;
            $cur_params['code'] = $cur_code;
            $cur_params['number'] = $cur_number;
            $cur_params['template_type'] = 0;
            $message->send($cur_params);
            // 保存验证码到缓存当中
            SFTools::setCache($cache_key, $cache_arr, 900);
        } else {
            $arr = $cache_arr['info'];
            $arr['seconds'] = $seconds < 10 ? '0' . $seconds : strval($seconds);
        }
        
        return $arr;
    }
    
    /**
     * 获取订单列明细
     * @param   string $fccode   订单号
     * @param   string $phone    电话号码
     * @param   string $cur_code 验证码
     * @param   string $pow_code 授权码
     * @return  array            数组对象数据
     */
    public function toRegisterBuyCode($fccode, $phone, $cur_code, $pow_code){
        if (empty($fccode)) {
            return SFTools::err("3903");
        }
        if (!preg_match(PREG_MATCH_PHONE, $phone)) {
            return SFTools::err("3311");
        }
        $cstMst = new \backend\model_erp\CstMst;
        $field = "fCCode,fCNName,fCName,fAddress,fTel";
        $cstMstFind = $cstMst::field($field)->where('fCCode', $fccode)->find();
        if (empty($cstMstFind)) {
            return SFTools::err("3902");
        }
        $fTel = trim($cstMstFind['fTel']);
        if (!preg_match(PREG_MATCH_PHONE, $fTel)) {
            return SFTools::err("3901");
        }
        // 生成 cache_key
        $cache_key = md5("LO{$fccode}{$phone}");
        // 获取缓存对象
        $cache_arr = SFTools::getCache($cache_key);
        if (empty($cache_arr)) {
            return SFTools::err("3904");
        }
        $err_code = "0000";
        if ($cache_arr['cur_code'] != $cur_code) {
            $err_code = "3905";
        }
        if (!empty($cache_arr['pow_code']) && $cache_arr['pow_code'] != $pow_code) {
            if ($err_code === "0000") {
                $err_code = "3906";
            } else {
                $err_code = "3907";
            }
        }
        if ($err_code !== "0000") {
            return SFTools::err($err_code);
        }
        $userAuthLogic = new \backend\logic\UserAuthLogic;
        $result = $userAuthLogic->saveUserAuthFindBuyFccodeAndPhone($fccode, $phone);
        if (empty($result['error_code'])) {
            SFTools::setCache($cache_key, NULL);
        }
        
        return $result;
    }
    
    /**
     * 获取订单列明细
     * @param   string $fccode 订单号
     * @param   string $phone  电话号码
     * @param   string $fTel   电话号码
     * @param   string $prefix 缓存前缀
     * @return  array          数组对象数据
     */
    public function getOauthFindBuyFccodeAndPhonefTel($fccode, $phone, $fTel, $prefix = "LLO"){
        if (empty($fccode)) {
            return SFTools::err("3903");
        }
        if (!preg_match(PREG_MATCH_PHONE, $phone)) {
            return SFTools::err("3311");
        }
        if (!preg_match(PREG_MATCH_PHONE, $fTel)) {
            return SFTools::err("3901");
        }
        // 生成 cache_key
        $cache_key = md5("{$prefix}{$fccode}{$phone}");
        // 获取缓存对象
        $cache_arr = SFTools::getCache($cache_key);
        $seconds = 60;
        if (!empty($cache_arr)) {
            $seconds = time() - intval($cache_arr['timestamp']);
        }
        if (empty($cache_arr) || $seconds > 60) {
            $sms_templates = config('sms_templates');
            $pow_number = SFTools::getRandStr(4, 3);
            $pow_phone = substr($fTel, 0, 3) . "*****" . substr($fTel, -3);
            $arr['pow_number'] = $pow_number;
            $arr['pow_phone'] = $pow_phone;
            $info['pow_number'] = $pow_number;
            $info['pow_phone'] = $pow_phone;
            $pow_code = SFTools::getRandStr(6, 1);
            $cache_arr['pow_code'] = $pow_code;
            // 发送授权人短信授权码
            $message = new \external\Message;
            $pow_params['phone'] = $fTel;
            $pow_params['code'] = $pow_code;
            $pow_params['number'] = $pow_number;
            $pow_params['fromPhome'] = $phone;
            $message->send($pow_params);
            $cache_arr['info'] = $info;
            $cache_arr['timestamp'] = time();
            // 保存验证码到缓存当中
            SFTools::setCache($cache_key, $cache_arr, 900);
        } else {
            $arr = $cache_arr['info'];
            $arr['seconds'] = $seconds < 10 ? '0' . $seconds : strval($seconds);
        }
        
        return $arr;
    }
}